package com.nidu.demo.mail.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.mail.convertor.MailAccountConvertor;
import com.nidu.demo.mail.convertor.MailAccountCriteriaConvertor;
import com.nidu.demo.mail.dto.MailAccountCO;
import com.nidu.demo.mail.dto.MailAccountPageQry;
import com.nidu.demo.mail.gateway.MailAccountGateway;
import com.nidu.demo.mail.model.MailAccount;
import com.nidu.demo.mail.model.MailAccountCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 邮件账号分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailAccountPageQryExe {

    private final MailAccountGateway gateway;
    private final MailAccountConvertor convertor;
    private final MailAccountCriteriaConvertor criteriaConvertor;

    public PageResponse<MailAccountCO> execute(MailAccountPageQry qry) {
        // 1. 转换查询条件
        MailAccountCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<MailAccount> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<MailAccountCO> mailAccountCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(mailAccountCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
